/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.progress;

import cn.ac.nercis.pes.common.constant.common.BaseConstants;
import cn.ac.nercis.pes.common.utils.ConvertUtils;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.AnchorPane;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 层面分数进度
 * @author zpy
 * @version 1.0.0
 */
public class LayerScoreProgressBar extends AnchorPane {
    private final static String SCORE_FORMAT = "%s/%s";
    private final ProgressBar progressBar;
    private final Label layerLbl;
    private final Label scoreLbl;

    private StringProperty layerName;

    private Float layerScore;

    public LayerScoreProgressBar(){
        this("层面指标",0f,0.0);
    }

    public LayerScoreProgressBar(String layerName,Float layerScore,Double evalScore){
        this.getStyleClass().add("layer-score");
        this.layerScore = layerScore;
        progressBar = new ProgressBar();
        progressBar.setProgress(0.5);
        AnchorPane.setLeftAnchor(progressBar,0.0);
        AnchorPane.setRightAnchor(progressBar,0.0);
        AnchorPane.setTopAnchor(progressBar,0.0);
        AnchorPane.setBottomAnchor(progressBar,0.0);

        layerLbl = new Label(layerName);
        layerLbl.textProperty().bind(layerNameProperty());
        AnchorPane.setTopAnchor(layerLbl,5.0);
        AnchorPane.setLeftAnchor(layerLbl,5.0);

        scoreLbl = new Label();
        scoreLbl.setTooltip(new Tooltip("测评得分/指标得分"));
        AnchorPane.setTopAnchor(scoreLbl,5.0);
        AnchorPane.setRightAnchor(scoreLbl,5.0);

        this.getChildren().addAll(progressBar,layerLbl,scoreLbl);

        this.setEvalScore(evalScore);
    }

    public final StringProperty layerNameProperty() {
        if (layerName == null) {
            layerName = new SimpleStringProperty(this, "层面指标", "");
        }
        return layerName;
    }

    public Float getLayerScore(){
        return this.layerScore;
    }

    public void setLayerScore(Float layerScore){
        this.layerScore = layerScore;
    }

    public String getLayerName(){
        return this.layerNameProperty().getValue();
    }

    public void setLayerName(String layerName){
        this.layerNameProperty().setValue(layerName);
    }

    public void setEvalScore(Double evalScore){
        var processValue = BigDecimal.valueOf(0);
        if(this.layerScore > 0){
            processValue = BigDecimal.valueOf((evalScore*layerScore)/layerScore).setScale(BaseConstants.SCORE_SCALE, RoundingMode.HALF_UP);
        }
        this.progressBar.setProgress(processValue.doubleValue());
        String scoreStr = String.format(SCORE_FORMAT,
                ConvertUtils.scoreToString(BigDecimal.valueOf((evalScore*layerScore)).setScale(BaseConstants.SCORE_SCALE,RoundingMode.HALF_UP).doubleValue()),
                ConvertUtils.scoreToString(this.layerScore,"0"));
        this.scoreLbl.setText(scoreStr);
    }


}